package socket.BIO_NIO_AIO.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class BIOServer {
    private static int DEDAULT_PORT = 8080;
    private static ServerSocket server;

    public void start() throws IOException {
        start(DEDAULT_PORT);
    }

    public void start(int port) throws IOException {
        synchronized(this) {
            if(server != null) {
                return;
            }

            try {
                //通过构造函数创建ServerSocket
                //如果端口合法且空闲，服务端就监听成功
                server = new ServerSocket(port);
                System.out.println("服务器已启动，端口号：" + port);
                //通过无线循环监听客户端连接
                //如果没有客户端接入，将阻塞在accept操作上。
                while(true){
                    Socket socket = server.accept();
                    //当有新的客户端接入时，会执行下面的代码
                    //然后创建一个新的线程处理这条Socket链路
                    new Thread(new BIOServerHandler(socket)).start();
                }
            } finally {
                if(server != null) {
                    System.out.println("服务器已关闭");
                    server.close();
                    server = null;
                }
            }
        }
    }
}
